/* 用户路由器文件 */

//引入模块
const { log } = require("console");
const express = require("express");
const fs = require("fs");
//引入连接池pool.js
const pool = require("../pool.js");
//引入上传文件upload.js
const { upload } = require("../upload/upload.js");
// 引入fileInfo
const { fileInfo, delDir } = require("../upload/flieInfo.js");
//创建路由器对象
const router = express.Router();
//登录接口 post(/user/login)
router.post("/login", function (req, res) {
  let $account = req.body.account; //登录账号
  let $password = req.body.password; //登录密码
  //非空验证
  if ($account === "") {
    res.send({
      code: 0,
      message: "登录账号不能为空",
    });
    return;
  }
  if ($password === "") {
    res.send({
      code: 0,
      message: "登录密码不能为空",
    });
    return;
  }
  let sql = "SELECT id,nickname FROM user WHERE account= ? AND password = ?"; //以登录账号与密码查询
  pool.query(sql, [$account, $password], function (err, result) {
    if (err) throw err; //抛出异常
    // 若查询无此账号，则返回登录失败
    if (result.length == 0) {
      res.send({
        code: 0,
        message: "登录账号或密码错误",
      });
    } else {
      //若有此账号,则返回登录成功
      let $id = result[0].id;
      let $nickname = result[0].nickname;
      res.send({
        code: 1,
        message: "登录成功",
        info: {
          id: $id,
          nickname: $nickname,
        },
      });
    }
  });
});

//注册接口 post(/user/register)
router.post("/register", function (req, res) {
  let $account = req.body.account;
  let $password = req.body.password;
  //非空验证
  if ($account === "") {
    res.send({
      code: 0,
      message: "注册账号不能为空",
    });
    return;
  }
  if ($password === "") {
    res.send({
      code: 0,
      message: "注册密码不能为空",
    });
    return;
  }
  // 查询用户账号是否已注册
  let sql = "SELECT id FROM user WHERE account = ?"; //以账号去查询
  pool.query(sql, [$account], function (err, result) {
    if (err) throw err; //抛出异常
    // 若查询无此账号用户，则写入数据库并返回注册成功
    if (result.length == 0) {
      // 生成用户昵称
      let $nickname = $account;
      let sql = "INSERT INTO user(account,password,nickname) VALUES(?,?,?);"; //将注册信息写入数据库
      pool.query(
        sql,
        [$account, $password, $nickname],
        function (err, results) {
          if (err) throw err; //抛出异常
          res.send({
            code: 1,
            message: "注册成功",
          });
        }
      );
    } else {
      //若有此账号,则返回用户已存在
      res.send({
        code: 0,
        message: "该用户已存在",
      });
    }
  });
});

//上传文件 post(/user/uploadFile)
router.post("/uploadFile", upload.single("uploadFile"), function (req, res) {
  // 上传的文件目录
  let $catalog = req.file.destination;
  let totalSize = fileInfo($catalog);
  // 文件库保存最大内存  2GB
  let maxSize = 2 * 1024 * 1024 * 1024;
  if (totalSize > maxSize) {
    // 移除文件夹
    delDir(dir);
    res.send({
      code: 0,
      msg: "内存不足,已清空,请重新上传",
    });
  } else {
    let name = req.file.filename;
    let url = "http://bigtree.host/uploadFile/" + name;
    let type = req.file.mimetype.split("/")[0];
    res.send({
      code: 1,
      msg: "上传成功",
      info: {
        name: name,
        url: url,
        type: type,
      },
    });
  }
});

//导出路由器对象，供web服务器使用
module.exports = router;
